package common.dao.Impl;

import cn.hutool.core.util.StrUtil;
import common.dao.AdminDao;
import common.domain.Admin;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(Admin admin) {
        String sql = "insert into admin\n" +
                "        (admin_name,user_id) \n" +
                "values(?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,admin.getAdminName());
            psmt.setLong(2,admin.getUserId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(Admin admin) {
        String sql = "update admin set";
        List<Object> param = new ArrayList<>();
        if (admin == null){
            return;
        }else{
            if(admin.getAdminId() != null){
                sql += " admin_id = ?,";
                param.add(admin.getAdminId());
            }
            if(admin.getAdminName() != null){
                sql += " admin_name = ?,";
                param.add(admin.getAdminName());
            }
            if(admin.getUserId() != null){
                sql += " user_id = ?,";
                param.add(admin.getUserId());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where admin_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,admin.getAdminId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from admin where admin_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public Admin selectById(Long id) {
        String sql = "select * from admin where admin_id = ?";
        Admin admin = new Admin();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                admin.setAdminId(rs.getLong(1));
                admin.setAdminName(rs.getString(2));
                admin.setUserId(rs.getLong(3));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return admin;
    }

    @Override
    public List<Admin> selectList(Admin admin) {
        String sql = "select * from admin where 1=1";
        List<Admin> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (admin == null){
            sql = "select * from admin";
        }else{
            if(admin.getAdminId() != null){
                sql += " and admin_id = ?";
                param.add(admin.getAdminId());
            }
            if(admin.getAdminName() != null){
                sql += " and admin_name like \"%\"?\"%\"";
                param.add(admin.getAdminName());
            }
            if(admin.getUserId() != null){
                sql += " and user_id = ?";
                param.add(admin.getUserId());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                Admin admin1 = new Admin();
                admin1.setAdminId(rs.getLong(1));
                admin1.setAdminName(rs.getString(2));
                admin1.setUserId(rs.getLong(3));
                list.add(admin1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
